package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;
/**
 * http://www.cntv.cn/ 央视网
 * @author allen
 */
public class CntvNewsAnalyse implements AnalyseNews {


	
	public boolean isDetailPage(String url) {
		boolean bRet=false;
		String[] regex={
				"http://.*.cntv.cn/[0-9]{4}/[0-9]{2}/[0-9]{2}/ARTI[0-9]*.shtml",
				"http://sannong.cntv.cn/[0-9]{8}/[0-9]*.shtml"
				};
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return bRet;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();
		news.setUrl(url);
		String title=null;
		String content=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);

		doc.select(".option_box").remove();
		doc.select(".con_w245").remove();
		doc.select(".jiaodian").remove();
		doc.select(".title_list_box").remove();
		doc.select(".md_hd").remove();
		doc.select("div[class^=ELMT]").remove();
		doc.select("div[class^=ind_B_edzt]").remove();


		Elements select=doc.select("h1");
		if(select!=null && select.size()!=0){
			title=select.get(0).text();
		}
		if(StringUtils.isBlank(title)){
			title=doc.select("div.top_title > h3").text();
			if(StringUtils.isBlank(title)){
				title=doc.select("span#bigTitle.biaoti").text();
				if(StringUtils.isBlank(title)){
					title=doc.select("span#bigTitle.biaoti").text();
					if(StringUtils.isBlank(title)){
						title=doc.select("div.text_box h2").text();
						if(StringUtils.isBlank(title)){
							title=StringUtils.substringBeforeLast(doc.select("title").text(), "_");
						}
					}
				}
			}
		}

		if (!StringUtils.containsAnyIgnoreCase(url, "video","VIDE")){
			content=HtmlCleaner.getContentHtml(url,doc.select(".content_brief"));
			if(StringUtils.isBlank(content)){
				content=HtmlCleaner.getContentHtml(url,doc.select("#content_body"));
			}
			if(StringUtils.isBlank(content)){
				content=HtmlCleaner.getContentHtml(url,doc.select("#contentbody1"));
			}
			if(StringUtils.isBlank(content)){
				content=HtmlCleaner.getContentHtml(url,doc.select("#md_major_article_content"));
			}
			if(StringUtils.isBlank(content)){
				content=HtmlCleaner.getContentHtml(url,doc.select("#content"));
			}
			if(StringUtils.isBlank(content)){
				content=HtmlCleaner.getContentHtml(url,doc.select("div.fl p"));
			}
			if(StringUtils.isBlank(content)){
				content=HtmlCleaner.getContentHtml(url,doc.select("div.text_box"));
			}
		}

		date=JSoupUtils.matchDate(doc,"发布时间");
		if(date==null){
			date=DateUtils.matchDate(doc.select("p.art-info").text());
			if(date==null){
				date=DateUtils.matchDate(doc.select("span.dcy_dt").text());
				if(date==null){
					date=DateUtils.matchDate(doc.select("div.status_bar span.date").text());
					if(date==null){
						date=DateUtils.matchDate(doc.select("div#content span.time").text());
						if(date==null){
							date=DateUtils.matchDate(url);
						}
					}
				}
			}
		}
		String author=JSoupUtils.matchAuthor(doc, "来源：");

		news.setTitle(StringUtils.trimSpace(title));
		news.setContent(StringUtils.trimSpace(content));
		news.setDate(date);


		news.setAuthor(author);
		return news;
	}
	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	public static void main(String[] args) {
		CntvNewsAnalyse a=new CntvNewsAnalyse();
		String url="http://sannong.cntv.cn/issuetest/20140618/112075.shtml";
		UrlMeta meta=CrawlHTML.responseToURL(url);
		System.out.println(a.isDetailPage(url));
		NewsMeta parserHtml=a.parserHtml(meta);
		System.out.println(parserHtml);
	}


	
	public boolean isNeedUpdate(){
		return false;
	}
}
